﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Sahit.iSoilHotel.Infrastructure.Web.Caching;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Models.Record;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Models.Room;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Models.Customer;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Models.Price;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Models.Filter;
using Webdiyer.WebControls.Mvc;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Proxys.Record;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Proxys.Room;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Proxys.Price;

namespace Sahit.iSoilHotel.Portal.Controllers
{
    public class RecordController : Controller
    {
        #region Members

        private readonly HttpContextBase httpContext;
        private readonly ITavernRecordServiceProxy tavernRecordServiceProxy;
        private readonly ITavernRoomServiceProxy tavernRoomServiceProxy;
        private readonly ITavernPriceServiceProxy tavernPriceServiceProxy;
        private readonly ICacheManager cacheManager;

        #endregion

        #region Constructors

        public RecordController(HttpContextBase httpContext,
            ITavernRecordServiceProxy tavernRecordServiceProxy,
            ITavernRoomServiceProxy tavernRoomServiceProxy,
            ITavernPriceServiceProxy tavernPriceServiceProxy,
            ICacheManager cacheManager)
        {
            this.httpContext = httpContext;
            this.tavernRecordServiceProxy = tavernRecordServiceProxy;
            this.tavernRoomServiceProxy = tavernRoomServiceProxy;
            this.tavernPriceServiceProxy = tavernPriceServiceProxy;
            this.cacheManager = cacheManager;
        }

        #endregion

        #region Get Actions

        [HttpGet]
        public ActionResult Index(int index = 1)
        {
            List<RecordListModel> models = this.tavernRecordServiceProxy.GetRecordsList(new RecordFilterModel() { RecordStatusIDs = new string[] { "1", "2" } }).OrderBy(r => r.StartDate).AsQueryable().ToPagedList(index, 2);
            if (Request.IsAjaxRequest())
            {
                return PartialView("RecordList", models);
            }
            return View(models);
        }

         [HttpGet]
         public ActionResult Reserve(Guid RoomID)
         {
             List<RoomTypeModel> roomTypes = this.tavernRoomServiceProxy.GetRoomType();
             var recordModel = new ReserveModel();
             recordModel.SetRoomTypeOptions(roomTypes);
             RoomModel room = this.tavernRoomServiceProxy.GetRoom(new RoomFilterModel() { ID = (Guid)RoomID }).FirstOrDefault();
             recordModel.Room = room;
             return View(recordModel);
         }

         [HttpGet]
         public ActionResult CheckIn(Guid? roomID,Guid? recordID)
        {
            var checkInModel = GetCheckInModel(roomID, recordID);
            return View(checkInModel);
        }

         private CheckInModel GetCheckInModel(Guid? roomID, Guid? recordID)
         {
             CheckInModel checkInModel = null;
             checkInModel = this.tavernRecordServiceProxy.GetRecords(new RecordFilterModel() { RoomID = roomID, RecordID = recordID }).FirstOrDefault();
             if (checkInModel == null)
             {
                 checkInModel = new CheckInModel();
             }
             List<RoomTypeModel> roomTypes = this.tavernRoomServiceProxy.GetRoomType();
             checkInModel.SetRoomTypeOptions(roomTypes);
             List<PriceCodeModel> priceCodes = this.tavernPriceServiceProxy.GetAllPriceCodes();
             checkInModel.SetPriceCodeOptions(priceCodes);
             return checkInModel;
         }

         [HttpGet]
         public ActionResult CheckOut(string recordIDs)
        {
            return View();
        }

        [HttpGet]
        public JsonResult GetRoomType()
        {
            List<RoomTypeModel> roomTypes = this.tavernRoomServiceProxy.GetRoomType();
            var roomTypesResult = new JsonResult();
            roomTypesResult.Data = roomTypes;
            roomTypesResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return roomTypesResult;
        }

        [HttpPost]
        public ActionResult SearchRecord(string recordStatusIDs, Guid? roomTypeID, string roomNumber)
        {
            RecordFilterModel filter = new RecordFilterModel() { RoomNumber = roomNumber, RoomTypeID = roomTypeID, RecordStatusIDs = String.IsNullOrEmpty(recordStatusIDs)?null:recordStatusIDs.Split(new char[] { '~' }) };
            PagedList<RecordListModel> records = this.tavernRecordServiceProxy.GetRecordsList(filter).OrderBy(r => r.StartDate).AsQueryable().ToPagedList(1,2);
            ViewBag.Filter = filter;
            return PartialView("RecordList", records);
        }

        [HttpGet]
        public ActionResult SearchRecord(string recordStatusIDs, Guid? roomTypeID, string roomNumber, int index = 1)
        {
            RecordFilterModel filter = new RecordFilterModel() { RoomNumber = roomNumber, RoomTypeID = roomTypeID, RecordStatusIDs = String.IsNullOrEmpty(recordStatusIDs) ? null : recordStatusIDs.Split(new char[] { '~' }) };
            PagedList<RecordListModel> records = this.tavernRecordServiceProxy.GetRecordsList(filter).AsQueryable().ToPagedList(index, 2);
            ViewBag.Filter = filter;
            return PartialView("RecordList", records);
        }
        #endregion

        #region Post Actions

        [HttpPost]
        public ActionResult Reserve(ReserveModel model)
        {
            tavernRecordServiceProxy.Reserve(model);
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult CheckIn(CheckInModel model)
        {
            tavernRecordServiceProxy.CheckIn(model);
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult CheckOut(CheckOutModel model)
        {
            tavernRecordServiceProxy.CheckOut(model);
            return RedirectToAction("Index");
        }
        #endregion
    }
}
